import java.io.Serializable;
import java.util.HashMap;

public class Item implements Serializable {
	private static final long serialVersionUID = 1L;
	int ulen;
	int[] users;
	byte[] votes;
	HashMap<Integer, Integer> idToIdx;

	Item() {
		ulen = 0;
		users = new int[32];
		votes = new byte[32];
		idToIdx = new HashMap<Integer, Integer>();
	}

	byte getVote(int inputItemID) {
		for (int v = 0; v < ulen; v++) {
			if (users[v] == inputItemID) {
				return votes[v];
			}
		}
		return -1;
	}

	public byte voteGet(int userItemID) {
		if (userItemID > users[ulen - 1]) {
			return -1;
		}
		for (int u = 0; u < ulen; u++) {
			if (users[u] == userItemID) {
				return votes[u];
			}
			if (userItemID < users[u]) {
				return -1;
			}
		}
		return -1;
	}

	void put(int userID, byte vote) {
		if (ulen > users.length - 1) {
			grow();
		}
		users[ulen] = userID;
		votes[ulen] = vote;
		idToIdx.put(Integer.valueOf(userID), Integer.valueOf(ulen));
		ulen++;
	}

	void shrink() {
		int[] temp = new int[ulen];
		System.arraycopy(users, 0, temp, 0, ulen);
		users = temp;
		temp = null;

		byte[] tempByte = new byte[ulen];
		System.arraycopy(votes, 0, tempByte, 0, ulen);
		votes = tempByte;
		tempByte = null;
	}

	void grow() {
		int[] temp = new int[ulen * 2];
		System.arraycopy(users, 0, temp, 0, ulen);
		users = temp;
		temp = null;

		byte[] tempByte = new byte[ulen * 2];
		System.arraycopy(votes, 0, tempByte, 0, ulen);
		votes = tempByte;
		tempByte = null;
	}

	void sort() {
		quicksort(0, ulen - 1);
	}

	void quicksort(int low, int high) {
		int i = low;
		int j = high;
		int pivot = users[low + (high - low) / 2];

		while (i <= j) {
			while (users[i] < pivot) {
				i++;
			}
			while (users[j] > pivot) {
				j--;
			}

			if (i <= j) {
				int tempItem = users[i];
				users[i] = users[j];
				users[j] = tempItem;

				byte tempVote = votes[i];
				votes[i] = votes[j];
				votes[j] = tempVote;

				i++;
				j--;
			}
		}

		if (low < j) {
			quicksort(low, j);
		}
		if (i < high) {
			quicksort(i, high);
		}
	}
}
